﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using RotovapPlatform.Domain.Business.Recipes.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace RotovapPlatform.UI.ViewModels.RecipeManagement
{
	/// <summary>
	/// 历史实验组信息视图模型
	/// </summary>
	public partial class HistoryExperimentGroupViewModel : ObservableObject
	{
		private readonly ILogger<HistoryExperimentGroupViewModel> _logger;

		/// <summary>
		/// 历史实验组列表
		/// </summary>
		[ObservableProperty]
		private ObservableCollection<ExperimentGroupInfo> _experimentGroups = new ObservableCollection<ExperimentGroupInfo>();

		/// <summary>
		/// 选中的实验组
		/// </summary>
		[ObservableProperty]
		private ExperimentGroupInfo _selectedExperimentGroup;

		/// <summary>
		/// 实验组中的实验列表
		/// </summary>
		[ObservableProperty]
		private ObservableCollection<ExperimentInfo> _experiments = new ObservableCollection<ExperimentInfo>();

		/// <summary>
		/// 选中的实验
		/// </summary>
		[ObservableProperty]
		private ExperimentInfo _selectedExperiment;

		/// <summary>
		/// 开始日期
		/// </summary>
		[ObservableProperty]
		private DateTime _startDate = DateTime.Now.AddDays(-30);

		/// <summary>
		/// 结束日期
		/// </summary>
		[ObservableProperty]
		private DateTime _endDate = DateTime.Now;

		/// <summary>
		/// 实验组名称筛选
		/// </summary>
		[ObservableProperty]
		private string _nameFilter = string.Empty;



		/// <summary>
		/// 是否显示删除确认对话框
		/// </summary>
		[ObservableProperty]
		private bool _isDeleteConfirmationOpen;

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="logger">日志记录器</param>
		//public HistoryExperimentGroupViewModel(ILogger<HistoryExperimentGroupViewModel> logger)
		//{
		//	_logger = logger;
		//	_logger.LogInformation("历史实验组信息视图模型已初始化");

		//	// 初始化命令
		//	SearchCommand = new RelayCommand(async () => await SearchExperimentGroupsAsync());
		//	ExportCommand = new RelayCommand(ExportExperimentGroups, CanExportExperimentGroups);
		//	DeleteCommand = new RelayCommand<ExperimentGroupInfo>(ShowDeleteConfirmation);
		//	ConfirmDeleteCommand = new RelayCommand(async () => await DeleteExperimentGroupAsync());
		//	CancelDeleteCommand = new RelayCommand(CancelDelete);
		//	ViewExperimentsCommand = new RelayCommand<ExperimentGroupInfo>(ViewExperiments);
		//}

		/// <summary>
		/// 搜索命令
		/// </summary>
		public ICommand SearchCommand { get; }

		/// <summary>
		/// 导出命令
		/// </summary>
		public ICommand ExportCommand { get; }

		/// <summary>
		/// 删除命令
		/// </summary>
		public ICommand DeleteCommand { get; }

		/// <summary>
		/// 确认删除命令
		/// </summary>
		public ICommand ConfirmDeleteCommand { get; }

		/// <summary>
		/// 取消删除命令
		/// </summary>
		public ICommand CancelDeleteCommand { get; }

		/// <summary>
		/// 查看实验命令
		/// </summary>
		public ICommand ViewExperimentsCommand { get; }

		/// <summary>
		/// 初始化
		/// </summary>
		//	public async Task InitializeAsync()
		//	{
		//		try
		//		{
		//			IsLoading = true;
		//			_logger.LogInformation("正在初始化历史实验组数据...");

		//			// 这里将来会添加实际的数据加载逻辑
		//			await Task.Delay(500); // 模拟加载延迟

		//			// 模拟添加一些实验组数据
		//			await SearchExperimentGroupsAsync();

		//			_logger.LogInformation("历史实验组数据初始化完成");
		//		}
		//		catch (Exception ex)
		//		{
		//			_logger.LogError(ex, "初始化历史实验组数据时发生错误");
		//		}
		//		finally
		//		{
		//			IsLoading = false;
		//		}
		//	}

		//	/// <summary>
		//	/// 搜索实验组
		//	/// </summary>
		//	private async Task SearchExperimentGroupsAsync()
		//	{
		//		try
		//		{
		//			IsLoading = true;
		//			_logger.LogInformation($"正在搜索从 {StartDate:yyyy-MM-dd} 到 {EndDate:yyyy-MM-dd} 的历史实验组...");

		//			// 这里将来会添加实际的搜索逻辑
		//			await Task.Delay(500); // 模拟搜索延迟

		//			// 清空现有数据
		//			ExperimentGroups.Clear();

		//			// 模拟添加一些实验组数据
		//			var random = new Random();
		//			for (int i = 0; i < 10; i++)
		//			{
		//				var creationDate = StartDate.AddDays(random.NextDouble() * (EndDate - StartDate).TotalDays);
		//				var groupName = $"实验组-{i + 1:D2}";

		//				// 如果有名称筛选且不匹配，则跳过
		//				if (!string.IsNullOrEmpty(NameFilter) && !groupName.Contains(NameFilter))
		//				{
		//					continue;
		//				}

		//				ExperimentGroups.Add(new ExperimentGroupInfo
		//				{
		//					Id = $"EG-{DateTime.Now.Year}{DateTime.Now.Month:D2}{i + 1:D3}",
		//					Name = groupName,
		//					Description = $"这是实验组{i + 1}的描述信息，包含了该实验组的基本信息和目标。",
		//					CreationDate = creationDate,
		//					Creator = $"用户{random.Next(1, 5)}",
		//					ExperimentCount = random.Next(1, 6),
		//					Status = random.Next(3) == 0 ? "进行中" : (random.Next(2) == 0 ? "已完成" : "已取消")
		//				});
		//			}

		//			_logger.LogInformation($"搜索到 {ExperimentGroups.Count} 个历史实验组");
		//		}
		//		catch (Exception ex)
		//		{
		//			_logger.LogError(ex, "搜索历史实验组时发生错误");
		//		}
		//		finally
		//		{
		//			IsLoading = false;
		//		}
		//	}

		//	/// <summary>
		//	/// 导出实验组
		//	/// </summary>
		//	private void ExportExperimentGroups()
		//	{
		//		try
		//		{
		//			_logger.LogInformation("正在导出历史实验组数据...");

		//			// 这里将来会添加实际的导出逻辑

		//			_logger.LogInformation("历史实验组数据导出完成");
		//		}
		//		catch (Exception ex)
		//		{
		//			_logger.LogError(ex, "导出历史实验组数据时发生错误");
		//		}
		//	}

		//	/// <summary>
		//	/// 是否可以导出实验组
		//	/// </summary>
		//	private bool CanExportExperimentGroups()
		//	{
		//		return ExperimentGroups.Count > 0;
		//	}

		//	/// <summary>
		//	/// 显示删除确认对话框
		//	/// </summary>
		//	private void ShowDeleteConfirmation(ExperimentGroupInfo experimentGroup)
		//	{
		//		if (experimentGroup == null)
		//		{
		//			return;
		//		}

		//		SelectedExperimentGroup = experimentGroup;
		//		IsDeleteConfirmationOpen = true;
		//	}

		//	/// <summary>
		//	/// 删除实验组
		//	/// </summary>
		//	private async Task DeleteExperimentGroupAsync()
		//	{
		//		try
		//		{
		//			if (SelectedExperimentGroup == null)
		//			{
		//				return;
		//			}

		//			_logger.LogInformation($"正在删除实验组: {SelectedExperimentGroup.Id}");

		//			// 这里将来会添加实际的删除逻辑
		//			await Task.Delay(500); // 模拟删除延迟

		//			// 从列表中移除
		//			ExperimentGroups.Remove(SelectedExperimentGroup);

		//			// 关闭确认对话框
		//			IsDeleteConfirmationOpen = false;

		//			_logger.LogInformation($"实验组 {SelectedExperimentGroup.Id} 删除成功");
		//		}
		//		catch (Exception ex)
		//		{
		//			_logger.LogError(ex, $"删除实验组 {SelectedExperimentGroup?.Id} 时发生错误");
		//		}
		//	}

		//	/// <summary>
		//	/// 取消删除
		//	/// </summary>
		//	private void CancelDelete()
		//	{
		//		IsDeleteConfirmationOpen = false;
		//	}

		//	/// <summary>
		//	/// 查看实验组中的实验
		//	/// </summary>
		//	private void ViewExperiments(ExperimentGroupInfo experimentGroup)
		//	{
		//		if (experimentGroup == null)
		//		{
		//			return;
		//		}

		//		try
		//		{
		//			_logger.LogInformation($"正在查看实验组 {experimentGroup.Id} 中的实验");

		//			// 清空现有实验列表
		//			Experiments.Clear();

		//			// 模拟添加一些实验数据
		//			var random = new Random();
		//			for (int i = 0; i < experimentGroup.ExperimentCount; i++)
		//			{
		//				var startTime = experimentGroup.CreationDate.AddHours(random.Next(1, 24));
		//				var endTime = startTime.AddHours(random.Next(1, 8));

		//				Experiments.Add(new ExperimentInfo
		//				{
		//					Id = $"EXP-{experimentGroup.Id}-{i + 1:D2}",
		//					Name = $"实验-{i + 1:D2}",
		//					Description = $"这是实验组 {experimentGroup.Name} 中的第 {i + 1} 个实验",
		//					StartTime = startTime,
		//					EndTime = endTime,
		//					Status = random.Next(3) == 0 ? "进行中" : (random.Next(2) == 0 ? "已完成" : "已取消"),
		//					Operator = experimentGroup.Creator,
		//					RecipeName = $"配方-{random.Next(1, 10):D2}"
		//				});
		//			}

		//			_logger.LogInformation($"已加载 {Experiments.Count} 个实验");
		//		}
		//		catch (Exception ex)
		//		{
		//			_logger.LogError(ex, $"查看实验组 {experimentGroup.Id} 中的实验时发生错误");
		//		}
		//	}
		//}

		/// <summary>
		/// 实验组信息类
		/// </summary>
		public class ExperimentGroupInfo
		{
			/// <summary>
			/// 实验组ID
			/// </summary>
			public string Id { get; set; }

			/// <summary>
			/// 实验组名称
			/// </summary>
			public string Name { get; set; }

			/// <summary>
			/// 实验组描述
			/// </summary>
			public string Description { get; set; }

			/// <summary>
			/// 创建日期
			/// </summary>
			public DateTime CreationDate { get; set; }

			/// <summary>
			/// 创建者
			/// </summary>
			public string Creator { get; set; }

			/// <summary>
			/// 实验数量
			/// </summary>
			public int ExperimentCount { get; set; }

			/// <summary>
			/// 状态
			/// </summary>
			public string Status { get; set; }
		}

		/// <summary>
		/// 实验信息类
		/// </summary>
		public class ExperimentInfo
		{
			/// <summary>
			/// 实验ID
			/// </summary>
			public string Id { get; set; }

			/// <summary>
			/// 实验名称
			/// </summary>
			public string Name { get; set; }

			/// <summary>
			/// 实验描述
			/// </summary>
			public string Description { get; set; }

			/// <summary>
			/// 开始时间
			/// </summary>
			public DateTime StartTime { get; set; }

			/// <summary>
			/// 结束时间
			/// </summary>
			public DateTime EndTime { get; set; }

			/// <summary>
			/// 状态
			/// </summary>
			public string Status { get; set; }

			/// <summary>
			/// 操作员
			/// </summary>
			public string Operator { get; set; }

			/// <summary>
			/// 配方名称
			/// </summary>
			public string RecipeName { get; set; }
		}
	}
}
